package com.uplift.mobile.realty.controller;

import com.alibaba.fastjson.JSONObject;
import com.uplift.common.base.BusinessException;
import com.uplift.common.base.ServiceReslut;
import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper;
import com.uplift.common.result.ResultWrapper;
import com.uplift.mobile.realty.model.*;
import com.uplift.mobile.realty.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物业信息相关
 * 
 * @author bcwang
 *
 */
@Controller
@RequestMapping(value = "/realty")
public class RealtyController {

	@Autowired
	private IRealtyInfoService realtyInfoService;

	@Autowired
	private IRealtyRoomService realtyRoomService;

	@Autowired
	private IRealtyLeaseService realtyLeaseService;

	@Autowired
	private IRealtyFileService realtyFileService;

	@Autowired
	private IRealtyEarningsService realtyEarningsService;

	/**
	 * 查询物业信息
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public IResultable list(HttpServletRequest request, RealtyInfo realtyInfo) {
		try {
			List<Map<String, Object>> list = this.realtyInfoService.queryRealtyInfo(realtyInfo);
			return ResultWrapper.wrapListPage(list, ResultWrapper.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper.wrapListPage(null, ResultWrapper.FAIL, "查询失败");
		}
	}

	/**
	 * 新增物业记录
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public IResultable save(HttpServletRequest request, RealtyInfo realtyInfo) {
		try {
			ServiceReslut serviceReslut = this.realtyInfoService.addRealtyInfo(realtyInfo);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "保存成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "保存失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "保存失败");
		}
	}

	/**
	 * 删除物业记录
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove")
	@ResponseBody
	public IResultable remove(HttpServletRequest request, RealtyInfo realtyInfo) {
		try {
			ServiceReslut serviceReslut = this.realtyInfoService.deleteRealtyInfo(realtyInfo);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "删除成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "删除失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "删除失败");
		}
	}

	/**
	 * 删除房间记录
	 *
	 * @param request
	 * @param realtyRoomModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove-room")
	@ResponseBody
	public IResultable removeRoom(HttpServletRequest request, RealtyRoomModel realtyRoomModel) {
		try {
			ServiceReslut serviceReslut = this.realtyRoomService.deleteRealtyRoom(realtyRoomModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "删除成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "删除失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "删除失败");
		}
	}

	/**
	 * 更新物业记录
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public IResultable update(HttpServletRequest request, RealtyInfo realtyInfo) {
		try {
			ServiceReslut serviceReslut = this.realtyInfoService.updateRealtyInfo(realtyInfo);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "更新成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "更新失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "更新失败");
		}
	}

	/**
	 * 更新房间号记录
	 *
	 * @param request
	 * @param realtyRoomModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/update-room")
	@ResponseBody
	public IResultable updateRoom(HttpServletRequest request, RealtyRoomModel realtyRoomModel) {
		try {
			ServiceReslut serviceReslut = this.realtyRoomService.updateRealtyRoom(realtyRoomModel);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "更新成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "更新失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "更新失败");
		}
	}

	/**
	 * 获取单个物业信息
	 *
	 * @param request
	 * @param realtyInfo
	 * @return IResultable
	 */
	@RequestMapping(value = "/get-one")
	@ResponseBody
	public IResultable getOne(HttpServletRequest request, RealtyInfo realtyInfo, String type) {
		try {
			List<Map<String, Object>> list = this.realtyInfoService.queryRealtyInfo(realtyInfo);
			if (list!=null && list.size()>0) {

				Map<String, Object> data = list.get(0);

				if(StringUtils.equals(data.get("typeCode").toString(), "BUILD")) {
					RealtyRoomModel realtyRoomModel = new RealtyRoomModel();
					realtyRoomModel.setRealtyId(data.get("id").toString());

					if(StringUtils.equals(type, "2")) {
						realtyRoomModel.setIsShow("2");
					}

					list = this.realtyRoomService.queryRealtyRoom(realtyRoomModel);
					data.put("rooms", list);
				}

				return ResultWrapper.wrapMap(data, ResultWrapper.SUCC, "查询成功");
			} else {
				return ResultWrapper.wrapMap(null, ResultWrapper.SUCC, "查询成功");
			}
		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "查询失败");
		}
	}

	/**
	 * 获取房间租约信息
	 *
	 * @param request
	 * @param realtyLease
	 * @return IResultable
	 */
	@RequestMapping(value = "/get-lease")
	@ResponseBody
	public IResultable getLease(HttpServletRequest request, RealtyLease realtyLease) {
		try {
			return ResultWrapper.wrapMap(this.realtyLeaseService.getRealtyLease(realtyLease), ResultWrapper.SUCC, "查询成功");

		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "查询失败");
		}
	}

	/**
	 * 获取房间相关附件
	 *
	 * @param request
	 * @param realtyFileModel
	 * @return IResultable
	 */
	@RequestMapping(value = "/get-room-file")
	@ResponseBody
	public IResultable getRoomFile(HttpServletRequest request, RealtyFileModel realtyFileModel) {
		try {

			List<Map<String, Object>> typeFiles = realtyFileService.queryRealtyTypeFile(realtyFileModel);

			Map<String, Object> data = new HashMap<>();
			data.put("files", typeFiles);

			return ResultWrapper.wrapMap(data, ResultWrapper.SUCC, "查询成功");

		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "查询失败");
		}
	}

	/**
	 * 查看房间收益
	 *
	 * @param request
	 * @return IResultable
	 */
	@RequestMapping(value = "/room-earning")
	@ResponseBody
	public IResultable countRoomEarnings(HttpServletRequest request, RealtyEarnings realtyEarnings) {
		try {
			return ResultWrapper.wrapMap(this.realtyEarningsService.countEveMonthEarnings(realtyEarnings), ResultWrapper.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "查询失败");
		}
	}

	/**
	 * 查看总资产
	 *
	 * @param request
	 * @return IResultable
	 */
	@RequestMapping(value = "/count-asset")
	@ResponseBody
	public IResultable countAsset(HttpServletRequest request, RealtyEarnings realtyEarnings) {
		try {
			return ResultWrapper.wrapMap(this.realtyEarningsService.countCustomerAsset(realtyEarnings), ResultWrapper.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "查询失败");
		}
	}

	/**
	 * 导出租约明细
	 * @param realtyEarnings
	 * @return
	 */
	@RequestMapping(value = "/export-earnings")
	@ResponseBody
	public IResultable exportEarnings(RealtyEarnings realtyEarnings) {
		try {

			Map<String, String> data = new HashMap<>();
			data.put("pdf", this.realtyEarningsService.exportDetail(realtyEarnings));
			return ResultWrapper.wrapMap(data, ResultWrapper.SUCC, "导出成功");
		} catch (Exception e) {
			return ResultWrapper.wrapMap(null, ResultWrapper.FAIL, "导出失败");
		}
	}

}
